package com.gozap.dynmicrogram.example;

/**
 * 
 * 
 * @author yaoming
 * @date 2013-1-15 下午05:48:30
 * @since JDK1.6
 * @version 2.0.0
 */
public class MaxCommon {
	// common wszgr
	char[] arra = { 'c', 'w', 'd', 's', 'z', 'g', 'a', 'b', 'r' };
	char[] arrb = { 'h', 'w', 'h', 's', 'j', 'j', 'z', 'q', 'g', 'r' };

	public void maxCommon() {
		int lena = arra.length;
		int lenb = arrb.length;
		// 创建数组，并且进行默认值当其中一个为空，则最大公共值为空
		String[][] maxCommon = new String[lena + 1][lenb + 1];
		for (int i = 0; i < lenb + 1; i++) {
			maxCommon[0][i] = "";
		}
		for (int i = 0; i < lena + 1; i++) {
			maxCommon[i][0] = "";
		}

		String s1;
		String s2;

		// 循环使用a的值去和b中数据对比，对数组a来说下标为i，对maxcommon为i+1
		for (int a = 0; a < lena; a++) {

			for (int b = 0; b < lenb; b++) {

				if (arra[a] == arrb[b]) {
					maxCommon[a + 1][b + 1] = maxCommon[a][b] + arra[a];
				} else {

					s1 = maxCommon[a + 1][b];
					s2 = maxCommon[a][b + 1];
					if (s2.length() > s1.length()) {
						maxCommon[a + 1][b + 1] = s2;
					} else {
						maxCommon[a + 1][b + 1] = s1;
					}
				}

			}
		}

		System.out.println(maxCommon[lena][lenb]);
	}

	public static void main(String[] args) {
		MaxCommon common = new MaxCommon();
		common.maxCommon();
	}
}
